package com.jsh.erp.config;

import io.qdrant.client.QdrantClient;
import io.qdrant.client.QdrantGrpcClient;
import io.qdrant.client.grpc.Collections;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutionException;

@Configuration
public class EmdeddingConfig {

    @Value("${spring.vectorstore.qdrant.collection-name}")
    private String qdrantCollectionName;

    @Value("${spring.vectorstore.qdrant.host}")
    private String qdrantHost;

    @Value("${spring.vectorstore.qdrant.port}")
    private String qdrantPort;

    @Bean
    public QdrantClient getQdrantClient() throws ExecutionException, InterruptedException {
        QdrantClient qdrantClient = new QdrantClient(
                QdrantGrpcClient.newBuilder(qdrantHost, Integer.parseInt(qdrantPort), Boolean.parseBoolean("false")) // 构建一个gRPC客户端连接到"localhost"
                        .build());
        //判断知识库collection是否存在
        if (!qdrantClient.collectionExistsAsync(qdrantCollectionName).get()) {
            Collections.VectorParams vectorParams = Collections.VectorParams.newBuilder().setSize(Long.parseLong("768")).setDistanceValue(1).build();
            qdrantClient.createCollectionAsync(qdrantCollectionName, vectorParams);
        }
        return qdrantClient;
    }

}